# -*- coding: utf-8 -*-
# Created by methew at 2018/9/1
import requests
from constance import jqInfo, lunarMonthDesc, lunarDayDscr, jqjz, weekInfo


def isLeaf(lu):
    if lu.find('閏') > -1:
        return 1
    return 0


lunarYear = 1900
lunarMonth = '十一月'
lunarIsLeaf = 0
lunarDay = '十一'
lunarIsLeaf = 0
weekday = 2
f = open('f.txt', 'w')
f.truncate()


def wt(year, lunar_year, lunar_month, lunar_is_leaf):
    r = requests.get('http://www.hko.gov.hk/gts/time/calendar/text/T' + year + 'c.txt')
    r.encoding = 'big5'
    lineNo = 1
    arr = r.text.split('\n')
    for dayLine in arr:
        # print(dayLine)
        if lineNo > 3 and dayLine.find('年') > -1 and dayLine.find('夏令') == -1:
            # 获取公历数据
            gdate = dayLine[:15].strip().lstrip()
            gYear = gdate[:gdate.find('年')]
            gMonth = gdate[gdate.find('年') + 1:gdate.find('月')]
            gDay = gdate[gdate.find('月') + 1:gdate.find('日')]
            # 获取星期
            wk = dayLine[dayLine.find('星期'):dayLine.find('星期') + 5].strip().lstrip()
            # 节气在最后
            jq = dayLine[dayLine.find('星期') + 6:].strip().lstrip()

            if dayLine[14:28].strip().lstrip().find('月') > -1:  # 农历跨月
                # 获取月份
                lunar_month = dayLine[14:dayLine.find('月', 10) + 1].strip().lstrip()
                # 非闰月 月份直接获取
                if isLeaf(lunar_month) == 1:
                    # 闰月取后面部分
                    lunar_month = lunar_month[-(len(lunar_month) - 1):]
                    lunar_is_leaf = 1
                else:
                    lunar_is_leaf = 0
                # 如果是月份 当天是初一
                lunar_day = '初一'
                if lunar_month == '正月':
                    # 如果是第一月份 直接加一年
                    lunar_year = lunar_year + 1
            else:
                # dayLine.find('2053年12月9日') > -1 or dayLine.find('2056年3月15日') > -1 or dayLine.find('2063年7月25日')>-1
                if -1 < dayLine.find('星期') < 21:
                    lunar_day = '三十'
                else:
                    lunar_day = dayLine[14:28].strip().lstrip()
            # print(lunar_month)
            if jq == '':
                st = gYear + '.' + gMonth + '.' + gDay + '/' + str(lunar_year) + '.' + str(lunar_is_leaf) + '.' \
                     + str(lunarMonthDesc.index(lunar_month) + 1) + '.' + str(lunarDayDscr.index(lunar_day) + 1) + '/' + \
                     str(weekInfo.index(wk) + 1) + '/'
            else:
                st = gYear + '.' + gMonth + '.' + gDay + '/' + str(lunar_year) + '.' + str(lunar_is_leaf) + '.' \
                     + str(lunarMonthDesc.index(lunar_month) + 1) + '.' + str(lunarDayDscr.index(lunar_day) + 1) + '/' + \
                     str(weekInfo.index(wk) + 1) + '/' + str(jqInfo.index(jq) + 1)
            print(st)
            if gMonth == '12' and gDay == '31':
                f.write(st + '\n')
            else:
                f.write(st + ':')
                # lunar = dayLine[14:34].strip().lstrip()
                # wk = dayLine[34:50].strip().lstrip()
                # jq = dayLine[49:].strip().lstrip()
                # # print(str(year) + '月' + str(month) + '月' + str(day) + '日')

        lineNo = lineNo + 1
    return lunar_year, lunar_month, lunar_is_leaf


for year_s in range(1901, 2101):
    # print(lunarYear)
    lunarYear, lunarMonth, lunarIsLeaf = wt(str(year_s), lunarYear, lunarMonth, lunarIsLeaf)

f.close()
# day = arr[8][:15].strip().lstrip()
# lunar = arr[8][14:35].strip().lstrip()
# wk = arr[8][34:50].strip().lstrip()
# jq = arr[8][49:].strip().lstrip()
